package com.wdciti.sccp.iot.tsdata.core.model;

import com.wdciti.sccp.iot.tsdata.core.api.TsDataException;
import lombok.Data;
import lombok.NonNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JINWEN
 */
@Data
public class Template {
    @NonNull
    private String name;
    private boolean aligned = false;
    private Map<String, Measurement> measurements = new HashMap<>();

    public Template(String name, List<Measurement> measurements) {
        this.name = name;
        setAllMeasurement(measurements);

    }

    public Template(String name, Measurement... measurements) {
        this.name = name;
        setAllMeasurement(Arrays.asList(measurements));
    }

    private void setAllMeasurement(List<Measurement> measurements) {
        for (Measurement measurement : measurements) {
            this.measurements.put(measurement.getKey(), measurement);
        }
    }

    Measurement getMeasurement(String key) {
        Measurement measurement = measurements.get(key);
        if (measurement == null) {
            throw new TsDataException("Have Non Measurement of " + key);
        } else {
            return measurement;
        }
    }
}
